<?php
class product_admin extends controller{
	public $category;
	public function index(){
		$limit=25;
		$filter=array('category'=>0,'date'=>'');
		$paging=array('page_current'=>0,'number_affiche'=>$limit);
		if(isset($_POST)){
			foreach ($_POST as $key=>$value)
				$data[$key]=$value;
			$this->model('category_models_index');
		}
		if(isset($_POST['add_category'])){
			unset($data['add_category']);
			$this->category_models_index->add_categories($data);
		}
		if(isset($_POST['Update_Category'])){
			unset($data['Update_Category']);
			$this->category_models_index->update_categories($data);
		}
		if(isset($_POST['filter'])){
			$filter=$_POST['filter'];
		}
		$sort=isset($_POST['sort'])?$_POST['sort']:array();
		$adminMenu= $this->load->Module('adminMenu/index');
		$category=$this->load->Module('category/admin');

		$setting =$this->load->Module('setting/admin');
		$this->template=$this->config->config['admin_template'];
		
		$listAllProducts=$this->model()->get_list_product($filter,$sort,$paging);
		$option_filter_category=$category->wp_dropdown_cats($filter['category']);
		$this->load->helper('form');
		$option_filter_date=$this->model()->get_month_year_create();
		$tableCategoryHeader=array(0=>null,'name'=>'Name','slug'=>'url friendly','description'=>'Description','count'=>'Post');
		$category->listTableCategory();
		$data=array(
					'update_footer'=>'test',
					'admin_title'=>'Control Admin',
					'sort'=>$sort,
					'filter'=>$filter,
					'option_filter_category'=>$option_filter_category,
					'option_filter_date'=>$option_filter_date,
					'admin_menu'=>$adminMenu->generateAdminMenu(),
					'listAllProducts'=>$listAllProducts,
					'tableCategoryHeader'=>$tableCategoryHeader,
					'tableCategoryBody'=>$category->category,
					'totalActive'=>$this->model()->get_total_product(true),
					'total'=>$this->model()->get_total_product()
				);
		
		
		$this->view(
				array('admin-header',
						'product',
						'admin-footer'), 
				$data); // Display the page
	}
	public function new_product(){
		$adminMenu= $this->load->Module('adminMenu/index');
		$category=$this->load->Module('category/admin');
		$this->template='admin';
		$option_category=$category->wp_dropdown_cats();
		$option_top_category=$category->check_list_top_cats();
		$currentcat=array();//need to remplir
		$check_list_cats = $category->check_list_cats( $currentcat );
		
		$tableCategoryHeader=array(0=>null,'name'=>'Name','slug'=>'url friendly','description'=>'Description','count'=>'Post');
		
		$data=array(
				'update_footer'=>'test',
				'admin_title'=>'Control Admin',
				'option_category'=>$option_category,
				'admin_menu'=>$adminMenu->generateAdminMenu(),
				'tableCategoryHeader'=>$tableCategoryHeader,
				'tableCategoryBody'=>$this->category,
				'check_list_cats'=>$check_list_cats,
				'option_top_category'=>$option_top_category
		);
		
		$this->view(
				array('admin-header',
						'new-product',
						'admin-footer'),
				$data); // Display the page
	}
	public function create(){
		$product=new product;
		foreach ($_POST as $nameInput=>$value){
			if(property_exists($product,$nameInput)){
				$product->$nameInput=$value;
			}
		}
		$product->prd_link=str_replace(".html", '', $product->prd_link).".html";
		$product->prd_price=$_POST['price_extra'][0];
		//prix extra
		$price_extra=array();
		foreach ($_POST['min_qty'] as $index=>$minQty){
			if(is_numeric(trim($minQty)) and isset($_POST['price_extra'][$index]) and trim($_POST['price_extra'][$index])){
				$priceUnit=trim($_POST['price_extra'][$index]);
				$price_extra[$minQty]=$priceUnit;
			}
		}
		$product->price_extra=$price_extra;
		
		//techinique
		$technic=array();		
		foreach ($_POST['prd_caracteristique_type'] as $index=>$caracteristique_type){
			if(trim($caracteristique_type) and isset($_POST['prd_caracteristique_nom'][$index]) and trim($_POST['prd_caracteristique_nom'][$index])){
				$caracteristique_nom=trim($_POST['prd_caracteristique_nom'][$index]);
				$technic[$caracteristique_type]=$caracteristique_nom;
			}
		}
		$product->technic=$technic;
		//photo
		if(isset($_SESSION['PhotoName'])){
			$product->photo=$_SESSION['PhotoName'];
			//unset($_SESSION['PhotoName']);
		}
		//date public (timestamp)
		$product->prd_date_public=strtotime($_POST['aa'].'-'.$_POST['mm'].'-'.$_POST['jj'].' '.$_POST['hh'].':'.$_POST['mn'].':00');
		
		$product->create();
	}
	public function deactive($id){
		$this->model()->deactive($id);
		$this->load->helper('url');
		redirect('product/admin');
	}
	public function reactive($id){
		$this->model()->reactive($id);
		$this->load->helper('url');
		redirect('product/admin');
	}

	public function edit($id){

		$adminMenu= $this->load->Module('adminMenu/index');
		$category=$this->load->Module('category/admin');
		$this->template='admin';
		$product = new product($id);
		$option_category=$category->wp_dropdown_cats();
		$option_top_category=$category->check_list_top_cats($product->category);
		$currentcat=array_keys($product->category);//need to remplir
		$check_list_cats = $category->check_list_cats( $currentcat );
		
		$tableCategoryHeader=array(0=>null,'name'=>'Name','slug'=>'url friendly','description'=>'Description','count'=>'Post');
		
		$data=array(
				'update_footer'=>'test',
				'admin_title'=>'Control Admin',
				'option_category'=>$option_category,
				'admin_menu'=>$adminMenu->generateAdminMenu(),
				'tableCategoryHeader'=>$tableCategoryHeader,
				'tableCategoryBody'=>$this->category,
				'check_list_cats'=>$check_list_cats,
				'product'=>$product,
				'option_top_category'=>$option_top_category
		);
		
		$this->view(
				array('admin-header',
						'edit-product',
						'admin-footer'),
				$data); // Display the page
	}
	
	
	private function checkURL(){
		if(isset($_POST) and isset($_POST['url'])){
			if($url=trim($_POST['url'])) echo $url;
		}
	}

}